//ImageEvaluate lib is to evaluate a image is focus or not
//there are 3 score: PeakArea, PeakNum, PeakScore
//cut parts of the image as a input, default center[512,512]
#pragma once
#include "opencv2/opencv.hpp" //cvThreshold
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"


class ImageFocus
{

public:
	ImageFocus(float calcareawidtb = 1024, float calcareaheight = 256,float Set_validArea = 1000, float Set_goodScore = 2.0, float Set_ScoreMaxDif = 0.5)
	{
		validArea = Set_validArea;
		goodScore = Set_goodScore;
		ScoreMaxDif = Set_ScoreMaxDif;
		CalcAreaWidth = calcareawidtb;
		CalcAreaHeight = calcareaheight;
	}
	~ImageFocus()
	{

	}
	void InitFocal();
	int getBestImageIndex(int &index, float &Score, int& imgCount, int mode=1); //mode=1 means onecolor, =2 means two color with 532-Type
	int getFocalPlane(int &index1, int &index2, int mode=0);

	int ImageSent(const cv::Mat &Image, int index, float Zvalue, float &Score, int& peakSize, double& instenty); //index from 1 start
	int FocalImage(const cv::Mat &Image, int index, float Zvalue, double &Dif);  //to find Focal plane
	int getCurPara(float &Score, float &Area, double &Dev);
	void setCalcSize(int width, int height)
	{
		CalcAreaWidth = width;
		CalcAreaHeight = height;
	}

	int FocusContinueFlag; //=1 minStep =2 maxStep =-1 unknow, =0 stop
protected:
	float maxDevScore;
	float mDevScore;
	int bestDevIndex; //if bestIndex=-1, then not found a goodImage
	int preIndex;
	int wrongDirection;
	int errorFlag;
	double preStdDev;
	double predifStdDev;
	double difStdDev;
	/////
	int All_bestIndex;
	float All_maxScore;
	float All_minArea;
private:
	void InitNewFocus();
	int AreaScore(); //get Area and Score from Image512
	void c4MidScore(int& peakSize, double& instensity);
	int getGoodImageIndexInfo(int &index, float &Score, int& imgCount);

	float validArea; //200
	float goodScore; //2.0
	float ScoreMaxDif;
	float CalcAreaWidth;//用于计算score的区域，先设置正方形区域
	float CalcAreaHeight;//计算采用长方形

	int imageCount;
	int m_ScoreStop;

	float m_preScore;
	float m_curArea;
	float m_curScore;
	double m_MeanImg;
	double m_stdDevImg;

	std::vector<double> pub_stdDevArr;
	std::vector<float> pub_score;
	std::vector<float> pub_Areas;
	std::vector<float> pub_Z;
	//std::vector < FocusInfo > ImgInfoArr;	
	cv::Mat m_Image512;
	cv::Mat b_m, b_st;
	std::vector<cv::Point2f> m_peak;
	std::vector<float> m_score;
	std::vector<float> m_Areas;
	//////////////
	int mFocalImgCount;
	int mStageFlag;
	int mPlus, mMinus;
	int mfocal1, mfocal2, upIndex;
	double mDifPre;
	double mVimgPre; //Dev
	std::vector<int> mVecFocal;
	std::vector<double> mVecDif;
	std::vector<double> mVecMean;
	std::vector<double> mVecDev;
};